package com.gogo.mall.controller;

import com.gogo.mall.entity.QyMember;
import com.gogo.mall.entity.QyOrder;
import com.gogo.mall.jwt.PassToken;
import com.gogo.mall.result.ResponseCode;
import com.gogo.mall.result.ResponseData;
import com.gogo.mall.service.QyOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (QyOrder)表控制层
 *
 * @author yangjing
 * @since 2021-09-02 09:35:33
 */
@RestController
@RequestMapping("order")
public class QyOrderController {
    /**
     * 服务对象
     */
    @Resource
    private QyOrderService qyOrderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public QyOrder selectOne(Integer id) {
        return this.qyOrderService.queryById(id);
    }

    /**
     * 获取根据状态获取订单列表
     * 订单状态1表示待支付，2表示已支付/待发货，3表示已完成 4取消 0表示全部
     * @param request
     * @param map
     * @return
     */
    @PostMapping("orderlist")
    @PassToken
    public ResponseData orderList(HttpServletRequest request, @RequestBody Map<String, Integer>map) throws ParseException {
        QyMember user = (QyMember) request.getAttribute("UserInfo");
        Integer status = map.get("status");
        Integer page = map.get("page");

        List<QyOrder> orders = qyOrderService.getOrdersByStateAndUserId(page, user.getId(), status);
        HashMap<String, Object> resMap = new HashMap<>();
        resMap.put("data",orders);
        resMap.put("total",orders.size());
        int last_page;
        if(orders.size()%10==0){
            last_page=orders.size()/10;
        }
        else
            last_page=orders.size()/10+1;
        resMap.put("last_page",last_page);
        return new ResponseData(ResponseCode.SUCCESS,resMap);
    }

    /**
     * 获取订单详情
     * @param map
     *
     */
    @PostMapping("orderdetail")
    @PassToken
    public ResponseData orderDetail(@RequestBody Map<String,Integer> map){
        QyOrder order = qyOrderService.queryById(map.get("orderid"));
        return new ResponseData(ResponseCode.SUCCESS,order);
    }
}
